package com.sakura.taos.tool.manipulator.service;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.taos.tool.manipulator.dao.SysDbLinkDao;
import com.sakura.taos.tool.manipulator.model.SysDbLink;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * H2主数据库,维护taos数据的连接信息
 * @Author: Sakura
 * @Date: 2021/1/28
 */
@Service
@DS("master")
public class SysDbLinkService extends ServiceImpl<SysDbLinkDao,SysDbLink> {

    @Resource
    private DynamicRoutingDataSource dataSource;

    /**
     * 检查连接状态,并设置
     * @param records
     */
    public void connectionStatusCensor( List<SysDbLink> records) {
        // 获取所有连接信息
        Map<String, DataSource> sources = dataSource.getCurrentDataSources();
        // 遍历需要检查的连接信息
        for (int i = 0; i < records.size(); i++) {
            SysDbLink dbLink = records.get(i);
            String key = dbLink.computePollName();
            DataSource dataSource = sources.get(key);
            // 如果取不出来连接, 说明没有连接
            dbLink.setConnect(dataSource!=null);
        }
    }
}
